<?
/*
-------------------------------
Script - EvaGb
-------------------------------
File - db.class.php
-------------------------------
Function - Класс для работы с БД
-------------------------------
*/
class db {
   private $link;
   public $pref=false;
   public $sqls;
    function __construct() {
	    $this->link = mysql_connect($GLOBALS['_gb_cnf']['db_host'],$GLOBALS['_gb_cnf']['db_user'],$GLOBALS['_gb_cnf']['db_pass']) or die(mysql_error());
        mysql_query('SET NAMES '.$GLOBALS['_gb_cnf']['db_encode']);
        mysql_select_db($GLOBALS['_gb_cnf']['db_name'],$this->link);
        $this->pref=$GLOBALS['_gb_cnf']['db_pref'];
    }
    function query($sql){
       $this->sqls++;
	   return mysql_query($sql);
   }
    function fetch_array($result){
		return mysql_fetch_assoc($result);
	}
    function result($result) {
       return @mysql_result($result,0);
    }
    function insert($table,$value) {
         if (!is_array($table) && !is_array($value)) {
             return $this->query("INSERT INTO ".$this->pref."_".$table." VALUES ($value)");
         }else if (!is_array($table) && is_array($value)) {
             $sql="INSERT INTO ".$this->pref."_".$table." VALUES ";
             for($i=0,$c=count($value);$i<$c;$i++) {
                $sql.="({$value[$i]})".($c-1!==$i?',':'');
             }
             return $this->query($sql);
         }
     }
    function update($table,$value) {
         if (!is_array($table) && !is_array($value)) {
             return $this->query("UPDATE ".$this->pref."_".$table." SET ".$value);
         }else if (!is_array($table) && is_array($value)) {
             for($i=0,$c=count($value);$i<$c;$i++) {
                if (!$this->query("UPDATE ".$this->pref."_".$table." SET ".$value[$i])) {
                    return false;
                }
             }
           return true;
         }
         return false;
     }
    function del($table,$value) {
         if (!is_array($table) && !is_array($value)) {
             return $this->query("DELETE FROM ".$this->pref."_".$table." WHERE ".$value);
         }else if (!is_array($table) && is_array($value)) {
             for($i=0,$c=count($value);$i<$c;$i++) {
                if (!$this->query("DELETE FROM ".$this->pref."_".$table." WHERE ".$value[$i])) {
                    return false;
                }
             }
           return true;
         }
         return false;
     }
}
?>
